#include <sys/types.h>
#include <sys/time.h>

// 计算时间间隔
float calculate_time_interval(struct timeval *tv_request, struct timeval *tv_reply)
{
    int sec_interval = tv_reply->tv_sec - tv_request->tv_sec;
    int usec_interval = tv_reply->tv_usec - tv_request->tv_usec;
    float ret_ms = ((float)sec_interval * 1000000 + usec_interval) / 1000;
    return ret_ms;
}

// 计算校验码
int calculate_checksum(u_int16_t *data, int byte_len_data)
{
    u_int16_t sum = 0;
    int m = byte_len_data % 2;
    // printf("\n");
    for (int i = 0; i < byte_len_data / 2; i++)
    {
        u_int16_t tmp = htons(*data);
        sum += tmp;
        if (sum < tmp)
        {
            sum++;
        }
        data++;
        // printf("%x %x\n", tmp, sum);
    }
    sum = ~sum;
    return sum;
}